package org.pybaldaj.logic;

import java.util.ArrayList;
import java.util.Iterator;

import pybaldaj.client.chat.ChatRoom;

/**
 * Class handling generic game. Type parameter is a type of field, e.g. RectField for rectangular one.
 * @author akademi4eg
 *
 */
public class Game {

	public Game (Field a_field)
	{
		chat = new ChatRoom();
		field = a_field;
	}
	
	public Field getField ()
	{
		return field;
	}
	/**
	 * Method for getting players. Index starts from 0 and is cyclic,
	 * e.g. 0,1,2,3,4,5,6,7 is treated like 0,1,2,3,0,1,2,3 in case of 4 players total.
	 * @param index of the player
	 * @return player standing by index
	 */
	public Player getPlayer (int index)
	{
		index = index % getPlayersNumber();
		return players.get(index);
	}
	
	/**
	 * @return player that should move now
	 */
	public Player getCurrentPlayer ()
	{
		return getPlayer(turn);
	}
	
	/**
	 * Advancing to the next round.
	 */
	public void advanceTurn ()
	{
		turn++;
	}
	
	/**
	 * 
	 * @return current round number.
	 */
	public int getRound ()
	{
		return 1 + turn / getPlayersNumber();
	}
	
	/**
	 * 
	 * @return number of players in game.
	 */
	public int getPlayersNumber ()
	{
		return players.size();
	}
	
	/**
	 * XXX Iterator or ListIterator?
	 * @return iterator for moving on all players
	 */
	public Iterator<Player> getPlayers ()
	{
		return players.iterator();
	}
	
	/**
	 * Adds a player to the game.
	 * @param pl for adding to the game
	 */
	public void addPlayer (Player pl)
	{
		players.add(pl);
	}
	
	public ChatRoom getChat ()
	{
		return chat;
	}
	
	private int turn;
	
	// TODO replace with ArrayDeque
	private ArrayList<Player> players;
	private Field field;
	private ChatRoom chat;
}
